package xperiment.metaphor.model.entity;

import xperiment.metaphor.model.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import metaphor.service.ServiceException;

/**
 * Job description
 * @author Mark Ashworth
 * @version 1.0.0
 */
//@Entity
//@Table(name="job_description")
public class JobDescription extends DomainObject implements PersistentMutable, Named {
    
    //@Id @GeneratedValue(strategy=GenerationType.AUTO)
    //@Column(name = "job_id", unique = true, nullable = false)
    private Long id = null;
    
    /* The name of the job */
    //@Column(name="job_name", nullable=false, unique=true, length=50)
    private String name;
    
	/** The purpose of the job */
    //@Column(name="purpose", nullable=true, unique=false, length=4096)
	private String purpose;

    /* Whether the job description is enabled */
    //@Column(name = "enabled", nullable = false)
    private boolean enabled = true;
    
    public JobDescription() {
        this(null, null);
    }
    
    public JobDescription(String name) {
        this(name, null);
    }
    
    public JobDescription(String name, String purpose) {
        super();
        setName(name);
        setPurpose(purpose);
    }
    
	@Override
    public Long getId() {
		return this.id;
	}

	@Override 
    public void setId(Long id) {
		Long old = getId();
        this.id = id;
        firePropertyChange("id", old, getId());
	}
	
	@Override 
    public String getName() {
		return this.name;
	}

    public void setName(String name) {
        String old = getName();
		this.name = name;
        firePropertyChange("name", old, getName());
	}

	/**
	 * The purpose of the job
	 * @return String
	 */
	public String getPurpose() {
		return purpose;
	}

	/**
	 * The purpose of the job
	 * @param purpose The new purpose description
	 */
	public void setPurpose(String purpose) {
        String old = getPurpose();
		this.purpose = purpose;
        firePropertyChange("purpose", old, getPurpose());
	}
    
    /**
     * Whether the job description is enabled
     * @return boolean
     */
    public boolean isEnabled() {
        return this.enabled;
    }
    
    /**
     * Whether the job description is enabled
     * @param enabled The new value
     */
    public void setEnabled(boolean enabled) {
        boolean old = isEnabled();
        this.enabled = enabled;
        firePropertyChange("enabled", old, isEnabled());
    }

    /**
     * Saves the job description to the persistent store
     * @throws xperiment.metaphor.model.ModelException If there was an error
     */
    public JobDescription save() throws ModelException {

        try {
            JobDescription d = (JobDescription) getDao().persist(this);
            this.id = d.getId();
            return this;
        } catch (ServiceException e) {
            throw new SaveException("Unable to save job description", e);
        }
    }

    /**
     * Returns the name of the job description as the class string representation
     * @return String
     */
    @Override
    public String toString() {
        return getName();
    }
    
    /**
     * &lt;job_description class="xperiment.metaphor.model.people.employee.JobDescription" id="1" name="Programmer" enabled="true" &gt;
     * &lt;purpose&gt;&lt;![CDATA[To programme stuff]]&gt;&lt;/purpose&gt;
     * &lt;/job_description&gt;
     * 
     * @return String
     */
    public String toXML() {
        return "<job_description class='" + getClass().getName() 
                + "' id='" + getId() 
                + "' name='" + getName()
                + "' enabled='" + isEnabled()
                + "' >"
                + (getPurpose() == null ? "" : "<purpose><![CDATA[" + getPurpose() + "]]></purpose>")
                + "</job_description>"
                ;
    }

    /**
     * Performs equality on the name of the job description
     * @param obj The other object
     * @return boolean
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final JobDescription other = (JobDescription) obj;
        if (this.name != other.name && (this.name == null || !this.name.equals(other.name))) {
            return false;
        }
        return true;
    }

    /**
     * Performs the hash code of the name of the description
     * @return int
     */
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 41 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }
}
